package cn.iocoder.yudao.module.vote.dal.mysql.userref;

import java.util.*;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
import cn.iocoder.yudao.module.vote.dal.dataobject.userref.VoteUserRefDO;
import org.apache.ibatis.annotations.Mapper;
import cn.iocoder.yudao.module.vote.controller.admin.userref.vo.*;

/**
 * 用户投票规则关联 Mapper
 *
 * @author moving
 */
@Mapper
public interface VoteUserRefMapper extends BaseMapperX<VoteUserRefDO> {

    default PageResult<VoteUserRefDO> selectPage(VoteUserRefPageReqVO reqVO) {
        return selectPage(reqVO, new LambdaQueryWrapperX<VoteUserRefDO>()
                .eqIfPresent(VoteUserRefDO::getUserId, reqVO.getUserId())
                .eqIfPresent(VoteUserRefDO::getVoteId, reqVO.getVoteId())
                .likeIfPresent(VoteUserRefDO::getGroupName, reqVO.getGroupName())
                .likeIfPresent(VoteUserRefDO::getUserName, reqVO.getUserName())
                .betweenIfPresent(VoteUserRefDO::getCreateTime, reqVO.getBeginCreateTime(), reqVO.getEndCreateTime())
                .orderByDesc(VoteUserRefDO::getId));
    }

    default List<VoteUserRefDO> selectList(VoteUserRefExportReqVO reqVO) {
        return selectList(new LambdaQueryWrapperX<VoteUserRefDO>()
                .eqIfPresent(VoteUserRefDO::getUserId, reqVO.getUserId())
                .eqIfPresent(VoteUserRefDO::getVoteId, reqVO.getVoteId())
                .likeIfPresent(VoteUserRefDO::getGroupName, reqVO.getGroupName())
                .likeIfPresent(VoteUserRefDO::getUserName, reqVO.getUserName())
                .betweenIfPresent(VoteUserRefDO::getCreateTime, reqVO.getBeginCreateTime(), reqVO.getEndCreateTime())
                .orderByDesc(VoteUserRefDO::getId));
    }

    default VoteUserRefDO getByVoteIdAndUserName(Integer voteId, String userName) {
        return selectOne(new LambdaQueryWrapperX<VoteUserRefDO>()
                .eqIfPresent(VoteUserRefDO::getVoteId, voteId)
                .eqIfPresent(VoteUserRefDO::getUserName, userName)
                .last("LIMIT 1")
        );
    }

    default List<VoteUserRefDO> getByVoteId(Integer voteId) {
        return selectList(new LambdaQueryWrapperX<VoteUserRefDO>()
                .eqIfPresent(VoteUserRefDO::getVoteId, voteId)
                .orderByDesc(VoteUserRefDO::getId));
    }
    default List<VoteUserRefDO> getByUserId(Long userId) {
        return selectList(new LambdaQueryWrapperX<VoteUserRefDO>()
                .eqIfPresent(VoteUserRefDO::getUserId, userId)
                .orderByDesc(VoteUserRefDO::getId));
    }

    default VoteUserRefDO getByUserIdAndVoteId(Integer voteId,Long userId) {
        return selectOne(new LambdaQueryWrapperX<VoteUserRefDO>()
                .eqIfPresent(VoteUserRefDO::getUserId, userId)
                .eqIfPresent(VoteUserRefDO::getVoteId, voteId)
                .orderByDesc(VoteUserRefDO::getId));
    }
}
